
package com.kugou.game.sdk.ui.dialog;

import com.kugou.game.sdk.utils.Rx;

import android.app.Activity;
import android.text.Spanned;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BaseConfirmDialog extends BaseSDKDialog {

    protected Activity activity;

    private Button btnOk, btnCancel;

    private TextView tvMessage, title;

    private ImageView ivClose, ivSuccessLogo;

    private View line, diver;

    private LinearLayout layout, contentLayout;

    public BaseConfirmDialog(Activity activity, int theme) {
        super(activity, theme);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        this.activity = activity;
        initView();
    }

    public BaseConfirmDialog(Activity activity) {
        this(activity, 0);
    }

    private void initView() {
        int layoutId = Rx.layout.kg_confirm_dialog;
        setContentView(layoutId);
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();

        layout = (LinearLayout) findViewById(Rx.id.kg_dialog_layout);

        contentLayout = (LinearLayout) findViewById(Rx.id.kg_layout_content);

        btnOk = (Button) findViewById(Rx.id.kg_common_dialog_btn_ok);
        if (btnOk == null) {
            throw new RuntimeException(
                    "Your content must have a ImageButton whose id attribute is "
                            + "'R.id.kg_common_dialog_btn_ok'");
        }
        btnCancel = (Button) findViewById(Rx.id.kg_common_dialog_btn_cancel);
        if (btnCancel == null) {
            throw new RuntimeException(
                    "Your content must have a ImageButton whose id attribute is "
                            + "'R.id.kg_common_dialog_btn_cancel'");
        }
        ivClose = (ImageView) findViewById(Rx.id.kg_iv_close);
        if (ivClose == null) {
            throw new RuntimeException("Your content must have a ImageView whose id attribute is "
                    + "'R.id.kg_iv_close'");
        }
        ivClose.setOnClickListener(mCloseClickListener);
        btnCancel.setOnClickListener(mCancelClickListener);
        tvMessage = (TextView) findViewById(Rx.id.kg_dialog_message_tv);
        title = (TextView) findViewById(Rx.id.kg_tv_dialog_title);
        line = findViewById(Rx.id.kg_dialog_line);
        diver = findViewById(Rx.id.kg_dialog_btn_diver);
        ivSuccessLogo = (ImageView) findViewById(Rx.id.kg_dialog_message_tv1);

        setOKBtnText("确定");
        setCancelText("取消");
    }

    private View.OnClickListener mCloseClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            dismiss();
        }
    };

    private View.OnClickListener mCancelClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            dismiss();
        }
    };

    public void setSuccessLogoVisibility(int visibility) {
        if (ivSuccessLogo != null) {
            ivSuccessLogo.setVisibility(visibility);
        }
    }

    /**
     * 设置关闭按钮的可见性
     * 
     * @param visibility
     */
    public void setCloseVisibility(int visibility) {
        ivClose.setVisibility(visibility);
    }

    public void setOnCloseClickListener(View.OnClickListener listener) {
        ivClose.setOnClickListener(listener);
    }

    public void setOKClickListener(View.OnClickListener listener) {
        btnOk.setOnClickListener(listener);
    }

    public void setCancelClickListener(View.OnClickListener listener) {
        btnCancel.setOnClickListener(listener);
    }

    /**
     * 设置对话框标题
     */
    public void setDialogTitle(String str) {
        title.setText(str);
    }

    /**
     * 设置取消按钮名称
     */
    public void setCancelText(String str) {
        btnCancel.setText(str);
    }

    /**
     * 设置ok按钮名称
     */
    public void setOKBtnText(String str) {
        btnOk.setText(str);
    }

    /**
     * 设置OK按钮是否显示
     * 
     * @param visibility
     */
    public void setOKBtnVisibility(int visibility) {
        btnOk.setVisibility(visibility);
        diver.setVisibility(View.GONE);
    }

    public void showNoBtn() {
        line.setVisibility(View.GONE);
        btnCancel.setVisibility(View.GONE);
        btnOk.setVisibility(View.GONE);
        diver.setVisibility(View.GONE);
    }

    /**
     * 设置Cancel按钮是否显示
     * 
     * @param visibility
     */
    public void setCancelBtnVisibility(int visibility) {
        btnCancel.setVisibility(visibility);
        diver.setVisibility(View.GONE);
    }

    public void setMessage(String message) {
        tvMessage.setText(message);
    }

    public void setMessage(Spanned message) {
        tvMessage.setText(message);
    }

    public void setMessage(int resid) {
        tvMessage.setText(resid);
    }

    public void setTextGravity(int gravity) {
        tvMessage.setGravity(gravity);
    }

    public void setContentGravity(int gravity) {
        contentLayout.setGravity(gravity);
    }

    /**
     * 设置确定按钮的背景图片
     */
    public void setOkBtnBackground(int resId) {
        btnOk.setBackgroundResource(resId);
    }

    public void setTextPadding(int padding) {
        tvMessage.setPadding((int) (padding * mContext.getResources().getDisplayMetrics().density),
                0, 0, 0);
    }

    /**
     * 设置对话框的高度
     * 
     * @param height
     */
    public void setDialogHeight(int height) {
        android.view.ViewGroup.LayoutParams params = layout.getLayoutParams();
        params.height = height;

        layout.setLayoutParams(params);
    }
}
